package handlers

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"sanrenx.com/xyxf/app/daos"
	"sanrenx.com/xyxf/app/services"
	"sanrenx.com/xyxf/pkg/app"
	"sanrenx.com/xyxf/pkg/e"
	"sanrenx.com/xyxf/pkg/orm"
	"sanrenx.com/xyxf/pkg/utils"
)

// @Tags 解惑
// @Summary 解惑列表
// @Accept application/json
// @Produce  json
// @Param page query integer true "页码"
// @Param order query string false "排序 same or praise"
// @Success 200 {object} app.Response
// @Router  /app/v2/issue [get]
func IssueGetPage(c *gin.Context) {
	var (
		dataSrv services.IssueSrv
		params  struct {
			PageNum int  `form:"page" binding:"required,number"`
			Solve   bool `form:"solve"`
		}
	)
	appG := app.Gin{C: c}
	//校验绑定参数
	if err := c.ShouldBind(&params); err != nil {
		app.MarkErrors(err)
		appG.Response(http.StatusBadRequest, e.InvalidParams, nil)
		return
	}

	pageNum := params.PageNum
	offset, size := utils.GetPage(pageNum)
	if res, err := dataSrv.GetPageIssueBySolve(int64(offset), int64(size), params.Solve); err == nil {
		//返回信息
		appG.Response(http.StatusOK, e.SUCCESS, map[string]interface{}{
			"page": pageNum,  //页码
			"size": len(res), //数量
			"list": res,      //文章列表
		})
		return
	}

	appG.Response(http.StatusInternalServerError, e.ErrorGetArticlesFail, nil)
}

// @Tags 解惑
// @Summary 问题详情
// @Accept application/json
// @Produce json
// @Param id query integer true "问题ID"
// @Success 200 {object} app.Response
// @Router  /app/v2/issue/details [get]
func IssueGetDetails(c *gin.Context) {

	var (
		issueSrv services.IssueSrv
		userSrv  services.UserSrv
		params   struct {
			IssueID uint64 `form:"id" binding:"required,number"`
		}
	)

	appG := app.Gin{C: c}
	//校验绑定参数
	if err := c.ShouldBind(&params); err != nil {
		app.MarkErrors(err)
		appG.Response(http.StatusBadRequest, e.InvalidParams, nil)
		return
	}

	//获取文章详情
	res, err := issueSrv.GetIssueById(params.IssueID)
	if err != nil {
		appG.Response(http.StatusInternalServerError, e.ErrorGetArticleFail, nil)
		return
	}

	//用户登录
	if uid, err := appG.GetUID(); err == nil {
		//获取关注状态
		if data, err := userSrv.GetFav(uid, params.IssueID, "issue"); err == nil {
			res.Fav = data
		}
	}

	//返回信息
	appG.Response(http.StatusOK, e.SUCCESS, res)
}

// @Tags 解惑
// @Summary 提交问题
// @Accept application/json
// @Produce json
// @Param title body body string true "问题"
// @Success 200 {object} app.Response
// @Router  /app/v2/issue [post]
func IssuePost(c *gin.Context) {
	var (
		issueSrv services.IssueSrv
		params   struct {
			Title string `json:"title" binding:"required"`
			Anon  bool   `json:"anon"`
		}
	)

	appG := app.Gin{C: c}
	//校验绑定参数
	if err := c.ShouldBindJSON(&params); err != nil {
		app.MarkErrors(err)
		appG.Response(http.StatusBadRequest, e.InvalidParams, nil)
		return
	}

	uid, _ := appG.GetUID()
	issueDao := daos.IssueDao{DB: orm.DB}
	issueID, err := issueDao.NewIssue(uid, params.Title, params.Anon)
	if err != nil {
		appG.Response(http.StatusInternalServerError, e.ErrorCountArticleFail, nil)
		return
	}

	_, _ = issueSrv.RefreshCacheIssueById(issueID)
	appG.Response(http.StatusOK, e.SUCCESS, nil)
}
